﻿#region copyright.anytao.com
// Copyright   : 2008, Anytao.com
// Author      : Anytao，http://www.anytao.com
// FileName    : Anytao.ISpace.DAL
// Release     : 2008/06/07 1.0
// Description : DAL for user
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data.Linq;
using Anytao.Common;
using Anytao.ISpace.Entity;

namespace Anytao.ISpace.DAL
{
    public class UserDAL : IUser
    {
        ISpaceDataContext ctx = new ISpaceDataContext();

        public IList<User> GetUsers()
        { 
            return (from u in ctx.Users
                   select u) as List<User>;
        }

        public IList<User> GetUsersByID(int userID)
        {
            return (from u in ctx.Users
                    where u.UserID == userID
                    select u) as List<User>;
        }

        public IList<User> GetUsersBySearch(string[] keywords)
        { 
            //
            IList<User> users = new List<User>();

            return users;
        }

        public User GetUser(int userID)
        {
            return ctx.Users.Single(u => u.UserID == userID); 
        }

        public User GetUser(string userName)
        {
            return ctx.Users.Single(u => u.UserName == userName);
        }

        public User GetUser(string userName, string password)
        {
            return ctx.Users.Single(u => u.UserName == userName && u.Password == Cryptography.GetMD5(password));
        }

        public void Insert(User user)
        {
            ctx.Users.InsertOnSubmit(user);
            ctx.SubmitChanges();
        }

        public void Delete(int userID)
        {
            User user = ctx.Users.Single(u => u.UserID == userID);
            ctx.Users.DeleteOnSubmit(user);

            ctx.SubmitChanges();        
        }

        public void Update(User user)
        {
            User tmpUser = ctx.Users.Single(u => u.UserID == user.UserID);
            tmpUser.UserID = user.UserID;
            tmpUser.UserName = user.UserName;
            tmpUser.LogoID = user.LogoID;
            tmpUser.RoleID = user.RoleID;
            tmpUser.Age = user.Age;
            tmpUser.Description = user.Description;
            tmpUser.Gender = user.Gender;
            tmpUser.CreateDate = user.CreateDate;

            ctx.SubmitChanges();
        }

        public bool IsInRole(string roleName)
        {
            //TODO: to do it.
            return false;
        }
    }
}
